Skip to main content

Phone Number

The purpose of these rules is to ensure that all phone numbers are entered in a consistent, standard format. By prepopulating the field with a + sign, we simplify the data entry process and ensure that the phone number format is valid according to backend requirements.

The validation rule is designed to only check the format of the phone number if data is entered. It does not require a phone number to be entered. The validation process prioritizes U.S. numbers, with an easy fallback for international formats. This ensures smooth data entry for both U.S. and international users, while maintaining compatibility with backend systems.


Basic Requirements for Phone Numbers

  • All phone numbers must start with a + sign followed by the country code.
  • For U.S. numbers, the country code is +1, followed by exactly 10 digits.
    • Example: +1 123 456 7890
  • International numbers can have 7 to 15 digits after the country code.
    • Example: +44 20 7946 0958 (UK) or +61 2 9876 5432 (Australia).

Default Behavior for New and Empty Records

  • The phone number field will be prepopulated with a + sign when a new record is created or if the field is empty.
  • Users only need to enter the country code and the phone number digits.
  • The system will automatically validate and save the phone number, ensuring that the + is always present before the country code.
  • On Save:
    • If the field only contains the + without digits, the system will remove the + and treat the field as empty (no number saved).
    • The phone number will always be saved with the + to ensure backend compatibility.

Detailed Validation Requirements

U.S. Numbers

  • The phone number must begin with +1.
  • After the +1, there should be exactly 10 digits, grouped as an area code (3 digits), local exchange (3 digits), and a line number (4 digits).
    • Example: +1 123 456 7890

International Numbers

  • The phone number must begin with a + sign, followed by the appropriate country code.
  • The number should include between 7 and 15 digits after the country code, depending on the country.
    • Example: +61 2 9876 5432 (Australia)

Allowed Separators

  • Spaces, dashes, and periods can be used as separators between different parts of the number for readability (e.g., +1 123-456-7890 or +1.123.456.7890).
  • Parentheses around area codes are also allowed (e.g., +1 (123) 456-7890).

Salesforce Field Validation Rule

To enforce these rules in Salesforce, we will implement a field validation rule for the phone number fields. Below is an example of a validation rule that matches the outlined requirements:

Validation Rule Code

warning

This is just a conceptual example - please audit the validation and adjust it to your specific coding standards and requirements.

NOT(
OR(
/* U.S. Numbers: +1 followed by 10 digits */
REGEX(Phone, "\\+1\\s?\\(?\\d{3}\\)?[-.\\s]?\\d{3}[-.\\s]?\\d{4}"),

/* International Numbers: + followed by 7 to 15 digits */
REGEX(Phone, "\\+\\d{1,3}\\s?\\(?\\d{1,4}\\)?[-.\\s]?\\d{1,4}[-.\\s]?\\d{1,9}")
)
)

Clarification

  • This validation rule does not make the phone number field required. It only validates the format if data is entered.
  • If the field is left empty or contains only a + sign, no validation will be enforced, and the record will be saved without a phone number. The + sign will be removed on save, making the field blank.
  • If a phone number is entered, it must follow either the U.S. format (+1 followed by 10 digits) or the international format (+ followed by 7 to 15 digits).

User Error Message

If the phone number doesn't meet the validation rules, the following U.S.-centric error message will appear, with a fallback for international numbers:

"Phone number must start with +1 and include 10 digits (e.g., +1 123 456 7890). For international numbers, start with + and include 7-15 digits."